# -*- coding: utf-8 -*-
import csv


def handle_csv_file(files: []):
    head = perfectmd = '|序号|封面|名称|描述|时长|类型|发布时间|爬虫项目|爬取时间|\n' \
                       '|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|\n'

    index = 0
    file_count = 0
    for file in files:

        with open(file, 'rU') as f:
            reader = csv.DictReader(f)
            for line in reader:
                index += 1
                line.update(id=index)
                # print(line)
                perfectmd += '|{id}|![{title}]({image})|{title}|[{desc}]({detailUrl})|{duration})|{type}|{pubDate}|{project}|{timestamp}|\n'.format(
                    **line)
                print('current index', index)
                if index % 100 == 0:
                    file_count += 1
                    file_name = r'all-product_{0:0>3}.md'.format(file_count)
                    save_contentmd(perfectmd, file_name)
                    perfectmd = head

                # break
        f.close()

    file_count += 1
    file_name = r'all-product_{0:0>3}.md'.format(file_count)
    save_contentmd(perfectmd, file_name)
    # print(perfectmd)


# 处理Aver的csv文件
def handle_aver_csv_file(files: [], md_count=5000):
    head = avermd = '|序号|头像|姓名|简介|爬虫项目|爬虫|爬取时间|\n' \
                    '|:---:|:---:|:---:|:---:|:---:|:---:|:---:|\n'
    md_name_patter = r'aver-all_{0:0>2}.md'

    index = file_count = 0  # 序号 文件切割数量
    for file in files:
        with open(file, 'rU') as f:
            reader = csv.DictReader(f)
            for line in reader:
                index += 1
                print('handle record index', index)
                line.update(id=index)
                avermd += '|{id}|![{starName}]({avatar})|{starName}|{intro}|{spiderProject}|{spiderName}|{timestamp}|\n'.format(
                    **line)
                if index % md_count == 0:
                    file_count += 1
                    md_file_name = md_name_patter.format(file_count)
                    save_contentmd(avermd, md_file_name)
                    avermd = head

        f.close()

    if head != avermd:
        file_count += 1
        md_file_name = md_name_patter.format(file_count)
        save_contentmd(avermd, md_file_name)


# 保存内容到文件中
def save_contentmd(content: str, save_file: str):
    with open(save_file, 'wb') as wf:
        wf.write(content.encode('UTF-8'))
    wf.close()


if __name__ == '__main__':
    # files = [r'perfect-all.csv', r'sone-ideapocket-all.csv']
    # handle_csv_file(files)
    aver_files = [r'aver-all.csv']
    handle_aver_csv_file(aver_files, md_count=200)
